//
//  myGradeViewController.m
//  DuBai
//
//  Created by darktm on 14-3-18.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "MyGradeViewController.h"

#import "TaskWithAddGoodsComments.h"

@interface MyGradeViewController ()<WYHttpTaskDelegate>

@end

@implementation MyGradeViewController

@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.commitBtn.layer.cornerRadius = 13.0f;
    
    self.txt.layer.borderColor = [[[UIColor darkTextColor] colorWithAlphaComponent:.2] CGColor];
    self.txt.layer.borderWidth = 1.0f;
    
    self.txt.layer.cornerRadius = 8.0f;
    
    self.txt.placeholder = @"对该宝贝的评价";
    self.txt.placeholderColor = [[UIColor darkTextColor] colorWithAlphaComponent:.4];
    
    for (int i = 1111; i <= 1115; i++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:i];
        [btn setImage:[UIImage imageNamed:@"setting_grade_btn_normal.png"] forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamed:@"setting_grade_btn_selected.png"] forState:UIControlStateNormal];
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.txt resignFirstResponder];
}

#pragma mark - request delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
//    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithAddGoodsComments class]]){
        [delegate commentSuccess];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    DLog(@"%@",obj.networkOp.url);
}

- (IBAction)on_btn_grade:(UIButton *)sender {
    
    int tag = sender.tag;
    for (int i = 1111; i <= tag; i++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:i];
        btn.selected = YES;
    }
    
    for (int i = tag + 1; i <= 1115; i++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:i];
        btn.selected = NO;
    }
}

- (IBAction)on_btn_commit:(id)sender {
    
    if(!self.txt.text.length){
        [UIAlertView showAlertViewWithTitle:@"请输入评价内容。"];
        return;
    }
    
    BOOL bGrade = NO;
    int grade = 0;
    for (int i = 1115; i >= 111; i--) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:i];
        if(btn.selected){
            bGrade = YES;
            grade = i - 1110;
            break;
        }
    }
    if(!bGrade){
        [UIAlertView showAlertViewWithTitle:@"请给该商品评分。"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    TaskWithAddGoodsComments *comment = [[TaskWithAddGoodsComments alloc] init];
    comment.goodid = @"5309b0bc2e2f940cec0041a9";
    comment.desc = self.txt.text;
    comment.grade = [NSString stringWithFormat:@"%d",grade];
    comment.delegate = self;
    [comment start];
    
}
@end
